package com.example.idlefish.HomeAndSearch

import SearchHistoryManager
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.icu.text.SimpleDateFormat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.idlefish.R
import java.util.Date
import java.util.Locale

class HistoryItemAdapter (
    private val items: MutableList<SearchRecord>, // 数据源
    private val onItemClick: (String) -> Unit, // 点击事件回调
    private val recyclerView: RecyclerView,
    private val context: Context
) : RecyclerView.Adapter<HistoryItemAdapter.HistoryItemViewHolder>() {

    // ViewHolder 定义
    class HistoryItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.history_item)
        val imag: ImageView = itemView.findViewById(R.id.imageView)
        val gap:TextView = itemView.findViewById(R.id.gap)
    }

    // 创建 ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryItemViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.detailitem, parent, false) // 替换为你的 item 布局文件
        return HistoryItemViewHolder(view)
    }

    // 绑定数据到 ViewHolder
    override fun onBindViewHolder(holder: HistoryItemViewHolder, position: Int) {
        val item = items[position]
        holder.textView.text = item.content

        val originalBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.detail_del)
        val scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, 50, 50, true)
        // 设置缩放后的图片
        holder.imag.setImageBitmap(scaledBitmap)
        holder.gap.text = formatTimeDifference(item.timestamp)

        holder.imag.setOnClickListener{
            val searchHistoryManager = SearchHistoryManager(context)
            searchHistoryManager.deleteSearchRecord(position)



            items.clear()
            items.addAll(searchHistoryManager.loadSearchHistory())
            notifyDataSetChanged()
        }
        holder.itemView.setOnClickListener {
            val data = item.content
            onItemClick(data) // 调用点击事件回调


        }
    }
    fun formatTimeDifference(timestamp: Long): String {
        // 获取当前时间的时间戳（毫秒）
        val currentTime = System.currentTimeMillis()
        val diff = currentTime - timestamp // 时间差，单位毫秒

        // 转换为秒、分钟、小时、天
        val seconds = diff / 1000
        val minutes = seconds / 60
        val hours = minutes / 60
        val days = hours / 24

        // 根据时间差返回不同的格式
        return when {
            seconds < 60 -> "刚刚"
            minutes < 60 -> "$minutes 分钟前"
            hours < 24 -> "$hours 小时前"
            days < 7 -> "$days 天前"
            else -> {
                // 如果超过一周，显示具体日期
                val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
                dateFormat.format(Date(timestamp))
            }
        }
    }



    // 获取数据项数量
    override fun getItemCount(): Int = items.size
}